package pers.zh.parking.data;

import pers.zh.parking.common.Const;
import pers.zjc.commonlibs.util.SPUtils;
import pers.zjc.commonlibs.util.StringUtils;

public class UserSettings {

    private SPUtils spUtils = SPUtils.getInstance(Const.Sp.SP_USER_SETTINGS);

    /**
     * 静态内部类实现模式（线程安全，调用效率高，可以延时加载）
     */
    private static class Holder {
        private static UserSettings INSTANCE = new UserSettings();
    }

    public static UserSettings getInstance() {
        return Holder.INSTANCE;
    }

    private UserSettings(){}

    private String userId;

    private String account;

    private String password;

    private String avatar;

    private String userName;

    private String description;

    private String role;

    private String login; // 0:false 1:true

    public String getUserId() {
        return userId == null ? spUtils.getString("userId") : userId;
    }

    public void setUserId(String userId) {
        spUtils.put("userId", userId);
        this.userId = userId;
    }

    public String getAccount() {
        return account == null ? spUtils.getString("account") : account;
    }

    public void setAccount(String account) {
        spUtils.put("account", account);
        this.account = account;
    }

    public String getPassword() {
        return password == null ? spUtils.getString("password") : password;
    }

    public void setPassword(String password) {
        spUtils.put("password", password);
        this.password = password;
    }

    public String getAvatar() {
        return avatar == null ? spUtils.getString("avatar") : avatar;
    }

    public void setAvatar(String avatar) {
        spUtils.put("avatar", avatar);
        this.avatar = avatar;
    }

    public String getUserName() {
        return userName == null ? spUtils.getString("userName") : userName;
    }

    public void setUserName(String userName) {
        spUtils.put("userName", userName);
        this.userName = userName;
    }

    public String getDescription() {
        return description == null ? spUtils.getString("description") : description;
    }

    public void setDescription(String description) {
        spUtils.put("description", description);
        this.description = description;
    }

    public String getRole() {
        return role == null ? spUtils.getString("role") : role;
    }

    public void setRole(String role) {
        spUtils.put("role", role);
        this.role = role;
    }

    public void setLogin(boolean login) {
        this.login = login ? "1" : "0";
        spUtils.put("login", login ? "1" : "0");
    }

    public String getLogin() {
        return login == null ? spUtils.getString("login") : login;
    }

    public boolean isLogin() {
        return StringUtils.equals(getLogin(), "1");
    }

    public void clear() {
        this.userId = "";
        this.account = "";
        this.password = "";
        this.avatar = "";
        this.description = "";
        this.role = "";
        this.userName = "";
        this.login = "0";
        spUtils.clear();
    }
}
